<?php

class MemGroupController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new MemGroup;
		//声明场景
		$model->scenario = 'editgroup';
		
		if(isset($_GET['type'])){
			$model->type = (int)($_GET['type']);
		}
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['MemGroup']))
		{
			$model->attributes=$_POST['MemGroup'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		//声明场景
		$model->scenario = 'editgroup';
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['MemGroup']))
		{
			$model->attributes=$_POST['MemGroup'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除会员组
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();
		
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}


	/**
	 * 会员组管理
	 */
	public function actionAdmin()
	{
		$model=new MemGroup('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['MemGroup']))
			$model->attributes=$_GET['MemGroup'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 实例化模型
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=MemGroup::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='mem-group-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

}
